﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NakovBlog
{
    public class BlogDTO
    {
        public int BlogId { get; set; }
        public string BlogText { get; set; }
        public string BlogTitle { get; set; }
        public string BlogAuthor { get; set; }
        public string Postdate { get; set; }
        public IEnumerable<ComentDTO> Coments { get; set; }
        public int numberOfCommnets { get; set; }

        public static BlogDTO ConvertFromBlog(Blog blogFromDb) 
        {
            BlogDTO newBlog = new BlogDTO();
            newBlog.BlogId = blogFromDb.BlogId;
            newBlog.BlogText = blogFromDb.BlogText;
            newBlog.BlogTitle = blogFromDb.BlogTitle;
            newBlog.BlogAuthor = blogFromDb.BlogAutor;
            newBlog.Postdate = blogFromDb.Postdate;
            List<ComentDTO> comms = new List<ComentDTO>();

            foreach (var comment in blogFromDb.Coments)
            {
                comms.Add(ComentDTO.ConverFromComent(comment));
            }

            newBlog.Coments = comms;
            newBlog.numberOfCommnets = newBlog.Coments.Count();
            return newBlog;
        }

        public Blog ToBlog(BlogDTO blog) 
        {
            Blog newBlog = new Blog();
            newBlog.BlogId = blog.BlogId;
            newBlog.BlogText = blog.BlogText;
            newBlog.BlogTitle = blog.BlogTitle;
            newBlog.BlogAutor = blog.BlogAuthor;
            newBlog.Postdate = blog.Postdate;
            return newBlog;
        }
    }
}